package com.zeratul.demo.controller;

import com.zeratul.common.core.result.R;
import com.zeratul.common.event.base.serial.BaseEventSerialization;
import com.zeratul.common.event.core.apply.publish.ZtEventPublish;
import com.zeratul.common.event.enums.Qos;
import com.zeratul.demo.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/event")
@Slf4j
public class EventController {

    @Autowired
    private BaseEventSerialization eventSerialization;

    /**
     * 发布事件总线
     */
    @RequestMapping("/publish")
    public R publish() {
        log.info("v1发布普通消息");
        ZtEventPublish.sendQuickly("test-topic", "Hello World");
        return R.success("OK");
    }

    /**
     * 发布事件总线
     */
    @RequestMapping("/v2/publish")
    public R<String> publish2() {
        log.info("v2发布复杂消息");
        //创建一个User对象的集合，设置一些数据
        User user = new User();
        user.setType(1);
        user.setUsername("张三");
        user.setPassword("123456");
        ZtEventPublish.sendQuickly("demo-topic", user);
        return R.success("OK");
    }

    /**
     * 发送延迟消息
     */
    @RequestMapping("/v3/publish")
    public R<String> publish3() {
        log.info("v3发布延迟消息");
        //创建一个User对象的集合，设置一些数据
        User user = new User();
        user.setType(10);
        user.setUsername("王五");
        user.setPassword("111111");
        ZtEventPublish.sendDelay("demo-topic", user, Qos.LOW, 10, TimeUnit.SECONDS);
        return R.success("OK");
    }
}
